﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Barbar.UrlGuard.Controls {
  public class UrlContextMenu : ContextMenu, ILocalizable {
    private MenuItem m_AddUrl;
    private MenuItem m_EditUrl;
    private MenuItem m_RemoveUrl;
    private MenuItem m_ShowHistory;
    private MenuItem m_ShowHistoryGraph;

    public event EventHandler<UrlContextMenuEventArgs> Command;

    public UrlContextMenu() {

      m_AddUrl = new MenuItem("Add new url", OnMenuClick);
      m_AddUrl.Tag = UrlContextMenuCommand.AddUrl;

      m_EditUrl = new MenuItem("Edit url", OnMenuClick);
      m_EditUrl.Tag = UrlContextMenuCommand.EditUrl;

      m_RemoveUrl = new MenuItem("Remove this url", OnMenuClick);
      m_RemoveUrl.Tag = UrlContextMenuCommand.RemoveUrl;

      m_ShowHistory = new MenuItem("Show history", OnMenuClick);
      m_ShowHistory.Tag = UrlContextMenuCommand.ShowHistory;

      m_ShowHistoryGraph = new MenuItem("Show history as graph", OnMenuClick);
      m_ShowHistoryGraph.Tag = UrlContextMenuCommand.ShowHistoryGraph;

      MenuItems.AddRange(new MenuItem[] { m_AddUrl, m_EditUrl, m_RemoveUrl, m_ShowHistory, m_ShowHistoryGraph });
    }

    private void OnMenuClick(object sender, EventArgs e) {
      if (Command != null) {
        MenuItem menuItem = (MenuItem)sender;
        UrlContextMenuCommand command = (UrlContextMenuCommand)menuItem.Tag;
        Command(this, new UrlContextMenuEventArgs(command));
      }
    }

    #region ILocalizable Members

    public void Localize(ILocalizator localizator) {
      if (localizator == null)
        throw new ArgumentNullException("localizator");
      
      m_AddUrl.Text = localizator.Translate("TreeCommand_AddUrl");
      m_RemoveUrl.Text = localizator.Translate("TreeCommand_RemoveUrl");
      m_EditUrl.Text = localizator.Translate("TreeCommand_EditUrl");
      m_ShowHistory.Text = localizator.Translate("TreeCommand_ShowHistory");
      m_ShowHistoryGraph.Text = localizator.Translate("TreeCommand_ShowHistoryGraph");
    }

    #endregion
  }
}
